<template>
  <div class="container">
     <form action="/">
        <van-search
        class="nav_bar"
        v-if="$route.path==='/search'"
          v-model="searchText"
          show-action
          placeholder="请输入搜索关键词"
          @search="onSearch"
          @cancel="$router.back()"
          @focus="$emit('showResult')"
          @input="$emit('changeText',searchText)"
        />
      </form>
    <van-nav-bar
    v-if="$route.path!=='/search'"
      class="nav_bar"
      :class="$route.path==='/home'?'btn':''"
      :left-arrow="$route.path==='/login'"
      @click-left=$router.back()
    >
      <span slot="title" v-if="$route.path==='/login'">{{title}}</span>
      <van-button class="van_btn" round slot="title" @click="$router.push('/search')" v-if="$route.path==='/home'">{{title}}</van-button>
    </van-nav-bar>
    <!-- <h1>{{title}}</h1> -->
  </div>
</template>

<script>
export default {
  name: "Header",
  mounted() {
    this.$bus.$on('search',(c)=>{
      this.searchText =c
      // console.log(1);
      this.onSearch(c)
    })
  },
  created() {},
  data() {
    return {
      searchText:''
    };
  },
  props: {
    title: {
      type: String,
    }
  },
  methods: {
     onSearch(val) {
       if(val.trim().length!==0){
       this.searchText =val
      //  console.log(val);
      //  this.searchText =val
      this.$emit('Result',val)
      }else{
      // this.$emit('Result',val)
        this.searchText=''
      }
    }
  },
};
</script>

<style lang="less" scoped>
 /deep/.nav_bar {
  background-color: #3296fa;
 .van-nav-bar__title{
        color: snow;
    }
    .van-nav-bar__arrow{
        color: snow;
    }

}
/deep/.btn{
  .van-nav-bar__title{
    max-width: unset;
  }
.van_btn{
        width: 277px;
        height: 32px;
        background: #5babfb;
         color: snow;
         border: unset;
         .van-button__text{
           font-size: 13px;
         }
    }
}
.van-search__action{
  color: snow;
}
</style>
